package com.logical.test;
import java.util.HashMap;
import java.util.Map;

/**
 * @author : wwl
 * @date : 2021/12/20 22:46:28
 */
public class TwoSum {
    public static void main(String[] args) {
        Solution solution = new TwoSum().new Solution();
        int[] nums = {2,7,11,15};
        int target = 9;
        int[] ints = solution.twoSum(nums, target);
        int i;
        for (i = 0; i < ints.length; i++) {
            System.out.print(" "+ints[i]);
        }
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public int[] twoSum(int[] nums, int target) {
            Map<Integer, Integer> map = new HashMap<>();
            for (int i = 0; i < nums.length; i++) {
                if (map.containsKey(target - nums[i])) {
                    return new int[]{map.get(target - nums[i]), i};
                }
                map.put(nums[i], i);
            }
            throw new IllegalArgumentException("No two sum solution");
        }
    }
//leetcode submit region end(Prohibit modification and deletion)
    

}
